-- 衡力持心
local henglichixi = fk.CreateSkill {
    name = "mcdh_henglichixi",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

henglichixi:addEffect("viewas",{
    pattern = ".|.|.|.|.|basic",
    prompt = function ()
        return "#mcdh_henglichixin-card"
    end,
    interaction = function(self,player)
        local names = {}
        local mark = player:getTableMark("mcdh_henglichixin-round")
        for _, id in ipairs(Fk:getAllCardIds()) do
            local card = Fk:getCardById(id)
            if ((Fk.currentResponsePattern == nil and player:canUse(card)) or
                    (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
                if card.type == Card.TypeBasic and not table.contains(mark, card.trueName) then
                    table.insertIfNeed(names, card.name)
                end
            end
        end
        if #names == 0 then
            return false
        end
        return U.CardNameBox { choices = names}
    end,
    card_filter = function (self, player,to_select, selected)
        local card = Fk:getCardById(to_select)
        if #selected == 0 then
            return table.contains(player:getHandlyIds(true), to_select)
        elseif #selected <=3 then
            return table.contains(player:getHandlyIds(true), to_select) and Fk:getCardById(to_select).suit == Fk:getCardById(selected[1]).suit
        else
            return false
        end
        --查了下董白给的【铁索连环】能否被重铸，结果是可以，所以此处判例可以选择自己不能被弃置的牌
    end,
    before_use = function (self, player, use)
        local room = player.room
        local mark = player:getTableMark("mcdh_henglichixin-round")
        table.insert(mark, use.card.trueName)
        room:setPlayerMark(player, "mcdh_henglichixin-round", mark)
        local recast = {}
        for i=1,use.card:getMark(self.name) do
            if table.contains(player:getCardIds("h"), use.card:getMark(self.name..tostring(i))) then
                table.insert(recast,use.card:getMark(self.name..tostring(i)))
            end
        end
        if #recast ~= 0 then
            room:recastCard(recast,player,self.name)
        end
    end,
    after_use = function (self, player, use)
        if not player.dead then
            player.room:loseHp(player, 3-use.card:getMark(self.name), self.name)
        end
    end,
    view_as = function(self, cards)
        if not self.interaction.data or #cards < 1 or #cards > 3 then
            return nil
        end
        local card = Fk:cloneCard(self.interaction.data)
        card:setMark(self.name, #cards)
        for i=1,#cards do
            card:setMark(self.name..tostring(i), cards[i])
        end
        card.skillName = self.name
        return card
    end,
    enabled_at_play = function(self, player)
        return not player:isKongcheng() and not table.find(Fk:currentRoom().alive_players, function(p) return p.dying end)
    end,
    enabled_at_response = function(self, player, response)
        return not response and not player:isKongcheng() and not table.find(Fk:currentRoom().alive_players, function(p) return p.dying end)
    end,
})

Fk:loadTranslationTable {
    ["mcdh_henglichixin"] = "不息",
    [":mcdh_henglichixin"] = "『衡力持心』<br>每轮每种基本牌限一次，当你需要使用基本牌时，若没有角色处于濒死状态，你可以重铸至多三张花色相同的手牌，视为使用此基本牌，然后你失去3-X点体力（X为你重铸牌数）。",
    ["#mcdh_henglichixin-card"]="衡力持心:重铸至多三张花色相同的手牌",
}

return henglichixi